<?php


namespace App\Api\Hmyl;
use PhalApi\Api;
use PhalApi\Exception\BadRequestException;

/**
 * 韩美医疗同步设备
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class ItemNo extends Api
{

    public function getRules()
    {
        return array(
            'item' => array(),
        );
    }


    /**
     * 拉取泽恒设备
     */
    public function getItem($item){
        $frdm = 'A003918';
        $url = 'https://26er314622.goho.co/?s=App.Zhjlsb.Getsb';
        // 先实例
        $curl = new \PhalApi\CUrl();
        $data = array(
            'page' => 1,
            'size' => 10,
            'username' => $frdm,
            'where' => json_encode(array(
                    "factory_number" => $item['factoryNo'],
                    "management_number" => $item['itemNo'],
                )) 
        );
        $rs = $curl->post($url, $data , 9000);
        $arr = json_decode($rs, true);
        return $arr;
    }

    /**
     * 更新泽恒设备
     */
    public function addsb($id, $zlitem){
        $frdm = 'A003918';
        $url = 'https://26er314622.goho.co/?s=App.Zhjlsb.addsb';
        // 先实例
        $curl = new \PhalApi\CUrl();
        //创建发送的数据
        $postData = array(
            'id' => $id ? $id : '',
            'username' => $frdm,
            'table' => 'phalapi_instrument',
            'arr' => json_encode($zlitem),
        );
        $curl->setHeader(array('Content-Type' => 'application/application/json'));
        $rs = $curl->post($url, json_encode($postData), 9000);
        $res = json_decode($rs, true);
        return $res;
    }

    /**
     * 同步设备
     */
    public function item()
    {
        //有需要同步的设备
        $items = \PhalApi\DI()->notorm->zhjlgetworklist->where(  array( 'sync' => '0' ) )->fetchAll();
        if (!$items) {
            throw new BadRequestException('没有需要同步的设备');
        }

        foreach( $items as $key => $item ){
            $arr = $this->getItem($item);

            //如果不存在则，创建
            if ( $arr['data']['count'] == 0 ) {
                $zlitem = array(
                    'traceability_institution' => "泽恒计量检测（北京）有限公司",
                    'factory_number' => $item['factoryNo'],
                    'management_number' => $item['itemNo'],
                    'instrument_level' => "A",
                    'department' => "设备部",
                    'instrument_name' => $item['itemName'],
                    'manufacturer' => $item['manufacturer'],
                    'number_of_instruments' => '1',
                    'calibration_date' => date("Y-m-d", strtotime($item['planDate'])),
                    'calibration_cycle' => 12,
                    'expiration_progress' => 364,
                    'measurement_range' => $item['checkType'],
                    'validity_period' => date("Y-m-d", strtotime($item['planDate']) + 365*24*60*60 ),
                    'service_type' => "送检",
                    'model_specifications' => $item["specs"],
                    'maximum_level' => $item["outVerifyWrong"],
                    'remarks' => $item['outVerifyContent'],
                    'management_status' => "正常",
                    'person_in_charge' => "管理员",
                );
                $res = $this->addsb("", $zlitem);
                if ( $res['code'] == 200 ) {
                    \PhalApi\DI()->notorm->zhjlgetworklist->where(  array( 'id' => $item['id'] ) )->update( array( 'sync' => '1', 'zhid' => $res['id'] ) );
                }
            }

            if ( $arr['data']['count'] > 0 ) {

                $one = $arr['data']['data'][0];
                //如果存在则，更新
                $zlitem = array(
                    'remarks' => $item['outVerifyContent'],
                );
                $res = $this->addsb($one['id'], $zlitem);
                \PhalApi\DI()->notorm->zhjlgetworklist->where(  array( 'id' => $item['id'] ) )->update( array( 'sync' => '1', 'zhid' => $one['id'] ) );                
            }
        }

        return '设备同步完成';
    }
}
